import {memo, ReactNode} from 'react';
import {useSelector} from 'react-redux';
import {Navigate} from 'react-router-dom';
import usePathname from '@/components/hook/usePathname';
import {WHITE_OF_AUTH_ROUTE} from '@/router/config';
import {RootState} from '@/store';

const AuthRoute = memo<{children: ReactNode}>(function AuthRoute(props) {
    const {firstLevel} = usePathname();
    const {isLogin} = useSelector((state: RootState) => state.root);

    if (WHITE_OF_AUTH_ROUTE.includes(`/${firstLevel}`) || isLogin) {
        return <>{props.children}</>;
    }
    return <Navigate to="/login" />;
});

export default AuthRoute;
